package model

import (
	"context"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var _ AnswerModel = (*customAnswerModel)(nil)

var insertAnswerColumns = stringx.Remove(answerFieldNames,"id")

type (
	// AnswerModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAnswerModel.
	AnswerModel interface {
		answerModel
		withSession(session sqlx.Session) AnswerModel
		InsertBuilder()squirrel.InsertBuilder
		InsertGetId(ctx context.Context, session sqlx.Session, data *Answer) (int64, error)
	}

	customAnswerModel struct {
		*defaultAnswerModel
	}
)

// NewAnswerModel returns a model for the database table.
func NewAnswerModel(conn sqlx.SqlConn) AnswerModel {
	return &customAnswerModel{
		defaultAnswerModel: newAnswerModel(conn),
	}
}

func (m *customAnswerModel) withSession(session sqlx.Session) AnswerModel {
	return NewAnswerModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customAnswerModel) InsertGetId(ctx context.Context, session sqlx.Session, data *Answer) (int64, error) {
	query, args, err := m.InsertBuilder().Columns(insertAnswerColumns...).
		Values(data.CustomerId,data.ServeyId,data.SubmitTime,data.IsFinished,data.Duration,data.IpAddr).
		Suffix("RETURNING \"id\"").
		ToSql()
	if err != nil {
		return 0, err
	}
	var id int64
	if session != nil {
		err = session.QueryRowPartialCtx(ctx, &id, query, args...)
	} else {
		err = m.conn.QueryRowPartialCtx(ctx, &id, query, args...)
	}
	if err != nil {
		return 0, err
	}

	return id,nil
}

func (m *customAnswerModel) InsertBuilder()squirrel.InsertBuilder {
	return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).Insert(m.table)
}
